﻿$(function () {
    var url = new Url();
    var $Id = url.GetQueryString("orderId");
    var fOrder = new FinancialOrder();
    //绑定订单
    fOrder.BindDetail($Id);
    fOrder.IsAllowDeduct($Id); //调用划扣
    fOrder.GetStatus($Id);
    //fOrder.DeductFail($Id);//划扣失败
    //返回
    $(".btnReturn").bind('click', function () {
        history.go(-1);
    });
    //结算划扣款项
    $(".btnDeductOK").bind('click', function () {
        if (confirm("确定划扣？")) {
            fOrder.DeductOK($Id);
        }
    });
    //划扣
    $(".btnDeduct").bind("click", function () {
        if (confirm("确定划扣？")) {
            if (fOrder.IsAllowDeduct($Id) == 0) {
                fOrder.Deduct($Id);
            }
        }
    });
    //划扣退回
    $(".btnBack").bind("click", function () {
        if (confirm("确定退回？")) {
            if (fOrder.IsAllowDeduct($Id) == 0) {
                fOrder.DeductReturn($Id);
            }
        }
    });
    //划扣失败
    $(".btnFail").bind("click", function () {
        //fOrder.DeductFail($Id);
        $.ajax({
            type: "post",
            dataType: "json",
            data: { Id: $Id, type: 37, _: Math.random() },
            url: "../Handler/FinancialOrder.ashx",
            complete: function () {
            },
            success: function (result) {
                alert(result.Message);
            }
        });
    });
})

var FinancialOrder = function () { };

FinancialOrder.prototype = {
    BindDetail: function ($Id) {
        $.ajax({
            type: "post",
            url: "../Handler/FinancialOrder.ashx",
            dataType: "json",
            data: { Id: $Id, type: 4 },
            complete: function () {
            },
            success: function (data) {
                if (data.Data.Order) {
                    //$(".divUserId").text(data.Order.UserId);// 客户
                    $(".divAmount").text(data.Data.Order.Amount);
                    $(".divIncome").text(data.Data.Order.Income);//收益
                    $(".divTerm").text(data.Data.Order.Term);
                    $(".divInitialLoanDate").text(data.Data.Order.InitialLoanDate);//初始出借日期
                    $(".divPayBank").text(data.Data.Order.PayBankName);//划扣银行
                    $(".divDepositBank").text(data.Data.Order.DepositBank);//划扣开户行
                    $(".divRecAccName").text(data.Data.Order.RecAccName);
                    $(".divRecAccNum").text(data.Data.Order.RecAccNum);//收款账户号码
                    $(".divAgreeNum").text(data.Data.Order.AgreeNum);
                    $(".divLoanNum").text(data.Data.Order.OrderId);
                    $(".divProductType").text(data.Data.Order.Product);
                    if (data.Data.User) {
                        $(".divIDCard").text(data.Data.User.IDCard);
                        $(".divPhone").text(data.Data.User.Phone);
                        $(".divAddress").text(data.Data.User.Address);
                        $(".divUserId").text(data.Data.User.Name);
                    }
                }
            }
        });
    },
    Deduct: function ($Id) {
        $.ajax({
            type: "post",
            url: "../Handler/FinancialOrder.ashx",
            dataType: "json",
            data: { Id: $Id, type: 14 },
            complete: function () {
            },
            success: function (data) {
                if (data > 0) {
                    alert("划扣成功！");
                    $(".btnDeduct").css({ "background": "#dddddd" });
                    $(".btnBack").css({ "background": "#dddddd" });
                    location.href = "Handled.aspx?css=1031";
                }
                else {
                    alert("划扣失败！");
                }
            }
        });
    },
    IsAllowDeduct: function ($Id) {
        var $IsAllow = 1;
        //是否允许划扣，如果未划扣显示划扣按钮，已划扣隐藏划扣按钮
        $.ajax({
            type: "post",
            url: "../Handler/FinancialOrder.ashx",
            dataType: "json",
            data: { Id: $Id, type: 16 },
            async: false,
            cache: false,
            complete: function () {
            },
            success: function (data) {
                if (data == 0) {
                    $(".btnDeduct").css("display", "block");
                    $(".btnBack").css("display", "block");
                }
                else {
                    if (data == 1) {
                        //$(".btnDeduct").css("display", "none");
                        $(".btnDeduct").css({ "background": "#dddddd" });
                        $(".btnBack").css({ "background": "#dddddd" });
                    }
                }
                $IsAllow = data;
            }
        });
        return $IsAllow;
    },
    DeductReturn: function ($Id) {
        $.ajax({
            type: "post",
            dataType: "json",
            data: { Id: $Id, type: 24, _: Math.random() },
            url: "../Handler/FinancialOrder.ashx",
            complete: function () {
            },
            success: function (result) {
                alert(result.Message);
                if (result.Status) {
                    location.href = "Handled.aspx?css=1031";
                }
            }
        });
    },
    DeductOK: function ($Id) {
        var UserName = $(".divUserId").text();
        var DeductBankName = $(".divPayBank").text();
        var DeductBankAccount = $(".divRecAccNum").text();
        var Amount = $(".divAmount").text();
        $.ajax({
            type: "post",
            url: "../Handler/CreditMatchHandler.ashx",
            dataType: "json",
            data: { "type": 3, "OrderId": $Id, "DeductBankAccount": DeductBankAccount, "UserName": UserName, "AMOUNT": Amount, "DeductBankName": DeductBankName, _: Math.random() },
            cache: false,
            success: function (result) {
                if (result.Status) {
                    if (result.Data == "0000") {
                        alert(result.Message);
                        $(".btnDeductOK").css({ "background": "#dddddd" });
                        $(".btnDeduct").css({ "background": "#dddddd" });
                        $(".btnBack").css({ "background": "#dddddd" });
                        location.href = "Handled.aspx?css=1031";
                    }
                    else {
                        alert(result.Message);
                    }
                    window.location.reload();
                } else {
                    alert(result.Message);
                }
            }
        });
    },
    GetStatus: function ($Id) {
        $(".btnDeductOK").css("display", "none");
        $(".btnDeduct").css("display", "none");
        $(".btnBack").css("display", "none");
        $(".btnFail").css("display", "none");
        //status 0正常，1，正在处理中，2，失败，3，成功
        $.ajax({
            type: "post",
            url: "../Handler/CreditMatchHandler.ashx",
            dataType: "json",
            data: { OrderId: $Id, type: 4 },
            async: false,
            cache: false,
            success: function (result) {
                if (result.Status) {
                    if (result.Message == 0) {
                        $(".btnDeductOK").css("display", "block");
                    }
                    else if (result.Message == 1) {

                    }
                    else if (result.Message == 2) {
                        $(".btnFail").css("display", "block");
                        $(".btnBack").css("display", "block");
                    }
                    else if (result.Message == 3) {
                        $(".btnDeduct").css("display", "block");
                    }
                } else {
                    alert(result.Message);
                }
            }
        });
    }
}

function bindUserInfo($userId) {
    $.ajax({
        type: "post",
        url: "../Handler/FinancialUser.ashx",
        dataType: "json",
        data: { Id: $userId, type: 4 },
        complete: function () {
        },
        success: function (data) {
            if (data != null && data != "") {
                $(".divIDCard").text(data.IDCard);
                $(".divPhone").text(data.Phone);
                $(".divAddress").text(data.Address);
            }
        }
    });
}
